package ly.count.ohos.demo;

import ly.count.ohos.sdk.Countly;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ScrollView;
import ohos.global.configuration.Configuration;
import ohos.os.ProcessManager;

public class AbilityExampleCrashReporting extends Ability {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_example_crash_reporting);
        ScrollView sv = (ScrollView) findComponentById(ResourceTable.Id_sv);
        sv.enableScrollBar(ScrollView.AXIS_Y, true);
        findComponentById(ResourceTable.Id_button20)
            .setClickedListener(component -> onClickCrashReporting01(component));
        findComponentById(ResourceTable.Id_button21)
            .setClickedListener(component -> onClickCrashReporting02(component));
        findComponentById(ResourceTable.Id_button22)
            .setClickedListener(component -> onClickCrashReporting03(component));
        findComponentById(ResourceTable.Id_button23)
            .setClickedListener(component -> onClickCrashReporting04(component));
        findComponentById(ResourceTable.Id_button24)
            .setClickedListener(component -> onClickCrashReporting05(component));
        findComponentById(ResourceTable.Id_button25)
            .setClickedListener(component -> onClickCrashReporting06(component));
        findComponentById(ResourceTable.Id_button26)
            .setClickedListener(component -> onClickCrashReporting07(component));
        findComponentById(ResourceTable.Id_button27)
            .setClickedListener(component -> onClickCrashReporting08(component));
        findComponentById(ResourceTable.Id_button28).setClickedListener(component -> {
            onClickCrashReporting09(component);
        });
        findComponentById(ResourceTable.Id_button49).setClickedListener(component -> {
            onClickCrashReporting10(component);
        });

        findComponentById(ResourceTable.Id_button48).setClickedListener(component -> {
            onClickCrashReporting11(component);
        });
        findComponentById(ResourceTable.Id_button50)
            .setClickedListener(component -> onClickCrashReporting13(component));
        Countly.sharedInstance().onStart(this);
    }

    @SuppressWarnings("unused")
    void EmptyFunction_1() {
        // keep this here, it's for proguard testing
    }

    @SuppressWarnings("unused")
    void EmptyFunction_2() {
        // keep this here, it's for proguard testing
    }

    @SuppressWarnings("unused")
    void EmptyFunction_3() {
        // keep this here, it's for proguard testing
    }

    public void onClickCrashReporting01(Component v) {
        Countly.sharedInstance().crashes().addCrashBreadcrumb("Unrecognized selector crash");
    }

    public void onClickCrashReporting02(Component v) {
        Countly.sharedInstance().crashes().addCrashBreadcrumb("Out of bounds crash");

        // noinspection MismatchedReadAndWriteOfArray
        int[] data = new int[]{};
        data[0] = 9;
    }

    public void onClickCrashReporting03(Component v) {
        Countly.sharedInstance().crashes().addCrashBreadcrumb("Null pointer crash");
        Object[] objects = null;
        objects[0].getClass();
    }

    public void onClickCrashReporting04(Component v) {
        Countly.sharedInstance().crashes().addCrashBreadcrumb("Invalid Geometry crash");
    }

    public void onClickCrashReporting05(Component v) {
        Countly.sharedInstance().crashes().addCrashBreadcrumb("Assert fail crash");
    }

    public void onClickCrashReporting06(Component v) {
        Countly.sharedInstance().crashes().addCrashBreadcrumb("Kill process crash");
        ProcessManager.kill(ProcessManager.getPid());
    }

    public void onClickCrashReporting07(Component v) {
        Countly.sharedInstance().crashes().addCrashBreadcrumb("Custom crash log crash");
        Countly.sharedInstance().crashes().addCrashBreadcrumb("Adding some custom crash log");

        // noinspection UnusedAssignment,divzero
        @SuppressWarnings("NumericOverflow")
        int test = 10 / 0;
    }

    public void onClickCrashReporting08(Component v) {
        Countly.sharedInstance().crashes().addCrashBreadcrumb("Recording handled exception 1");
        Countly.sharedInstance().crashes().recordHandledException(new Exception("A custom error text"));
        Countly.sharedInstance().crashes().addCrashBreadcrumb("Recording handled exception 3");
    }

    public void onClickCrashReporting09(Component v) {
        Countly.sharedInstance().crashes().addCrashBreadcrumb("Unhandled exception info");
        throw new RuntimeException("A unhandled exception");
    }

    public void onClickCrashReporting10(Component v) {
        deepFunctionCall_1();
    }

    public void onClickCrashReporting13(Component v) {
        String largeCrumb = "dddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
            + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd";
        Countly.sharedInstance().crashes().addCrashBreadcrumb(largeCrumb);
    }

    public void onClickCrashReporting11(Component v) {
        recursiveDeepCall(3);
    }

    void deepFunctionCall_1() {
        deepFunctionCall_2();
    }

    void deepFunctionCall_2() {
        deepFunctionCall_3();
    }

    void deepFunctionCall_3() {
        Utility.DeepCall_a();
    }

    void recursiveDeepCall(int depthLeft) {
        if (depthLeft > 0) {
            recursiveDeepCall(depthLeft - 1);
        } else {
            Utility.AnotherRecursiveCall(3);
        }
    }

    @Override
    public void onStop() {
        Countly.sharedInstance().onStop();
        super.onStop();
    }

    @Override
    public void onConfigurationUpdated(Configuration configuration) {
        super.onConfigurationUpdated(configuration);
        Countly.sharedInstance().onConfigurationChanged(configuration);
    }
}
